/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;
using Shuriken.Core.Ranking;
using Shuriken.Base;

namespace Shuriken.Core.Store
{
    /// <summary>
    /// Base class for all items 
    /// </summary>
    /// <remarks>
    /// This class is created for implementing the proper mechanism for registering and manipulating 
    /// actions correctly. 
    /// 
    /// Need a mechanism to 
    /// - be able to add new subjects and new actions easily 
    /// - be able to override new subjects inherited actions
    /// - be able to disable inherited actions
    /// 
    /// It works as the following: 
    /// Adding new subjects done through subclassing an existing subject or an existing one. 
    /// The new subject will inherit all the actions from its parent and capable of 
    ///  - Add new actions to the existing ones 
    ///  - Modify existing actions by replacing the existing ones
    ///  - Disabling them by removing them from the action list. 
    /// 
    /// Each Subject has a list of actions that is stored at the action manager and can be retrieved using its type. 
    /// 
    /// Need to consider: 
    /// - how this can effect future features like
    ///     a. Browsing
    /// 
    ///        Browsing and Actions are not effecting each other. 
    /// 
    ///     b. Text edit 
    ///        Text edit field is going to be a subject own its own, so it will have its own actions.  
    ///     
    /// Best result can be achived by designing for many scenarios and 
    /// possibly fully specing them out
    /// - Folder browser
    /// - Music playing and browsing 
    ///   a. Browse artist 
    ///   b. Browse Genre 
    /// - Email handling 
    ///   a. Contacts
    /// </remarks>
    public abstract class BaseStoreItem : IStoreItem, ISearchable
    {
        /// <summary>
        /// Image not loaded 
        /// </summary>
        protected static string IMAGE_NOT_LOADED = string.Empty;
        protected static string DEFAULT_IMAGE = string.Empty;

        protected string m_ImageIndex = IMAGE_NOT_LOADED;

        public BaseStoreItem()
        {
        }

        /// <summary>
        /// Register parent's action for this type. 
        /// </summary>
        /// <remarks>
        /// Register parent's action for this type. These actions are coming 
        /// from the parent classes. 
        /// </remarks>
        public virtual void RegisterParentActions()
        {
            List<IAction> actions = new List<IAction>();
            Type regType = this.GetType();
            Type baseType = regType.BaseType;

            // BaseFolderItem is not going to have any actions 
            // associated to it, so let's skip this type
            if (baseType == typeof(BaseStoreItem))
            {
                return;
            }

            ActionManager.GetDefaultInstance().GetListOfActions(baseType, actions);
            System.Diagnostics.Debug.Assert(actions.Count != 0, "No Action was returned for parent type!");
            ActionManager.GetDefaultInstance().RegisterActions(regType, actions);            
        }

        public virtual void RegisterActions()
        {
        }

        /// <summary>
        /// Stores an image in Image Store
        /// </summary>
        /// <param name="image">Image to store</param>
        /// <returns>Index of image</returns>
        protected static string StoreIcon(System.Drawing.Icon icon)
        {
            return ImageStore.AddResourceImage(icon);
        }

        /// <summary>
        /// Image loaded
        /// </summary>
        /// <returns>true if the image is already loaded</returns>
        public bool IsImageLoaded()
        {
            return m_ImageIndex != IMAGE_NOT_LOADED;
        }

        #region IObject Members

        public abstract string Name
        {
            get;
        }

        public abstract string Description
        {
            get;
        }

        public abstract string Icon
        {
            get;
            set;
        }

        #endregion

        #region ISearchable Members

        public abstract double Rank
        {
            get; 
            set; 
        }

        public abstract MutableIndexSet HitMask
        {
            get;
            set; 
        }

        /// <summary>
        /// Last search term used to select this item 
        /// </summary>
        public string SearchTerm
        {
            get;
            set; 
        }

        /// <summary>
        /// Unique id
        /// </summary>
        public string UniqueIdentifier
        {
            get
            {
                return string.Format("{0}.{1}", this.GetType(), this.Name);
            }
        }

        #endregion

        #region IStoreable Members

        public abstract IStore Store
        {
            get; 
            set;
        }

        public abstract void ExecuteDefaultAction();

        public virtual List<IAction> GetListOfActions()
        {
            List<IAction> listOfActions = new List<IAction>();
            ActionManager.GetDefaultInstance().GetListOfActions(this.GetType(), listOfActions);
            return listOfActions;
        }

        /// <summary>
        /// Removes the item from its store.
        /// </summary>
        public void RemoveItemFromStore()
        {
            System.Diagnostics.Trace.Assert(Store != null, "Store cannot be null!");
            Store.RemoveItem(this);
            Store = null;
        }

        #endregion
    }
}
